﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Admin.Users
{
    public partial class List : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) {
                GetUsers();
            }
        }

        private void GetUsers() {
            var users = UserRepository.QueryOver().List();
            UserList.DataSource = users.OrderBy(u => u.DisplayName);
            UserList.DataBind();
        }

        protected void UserListItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }

            var rowUser = (User)e.Item.DataItem;
            var editLink = (HyperLink) e.Item.FindControl("EditLink");

            editLink.NavigateUrl = "~/Admin/Users/Edit.aspx?id=" + rowUser.Id;
        }
    }
}